From d58bf0736fa6d68a26e58df8114f199968761f35 Mon Sep 17 00:00:00 2001
From: Daniel Kolesa <daniel@octaforge.org>
Date: Sat, 11 Mar 2023 05:22:30 +0100
Subject: [PATCH] don't bother with fully qualified hostname early on

This prevents doing a DNS lookup during early init. We just skip
the DNS lookup during the first init, then possibly reinit it
conditionally based on if use_fqdn is true.
---
 lib/cfg.c                     |  2 +-
 lib/hostname.c                | 17 +++++++++++------
 lib/hostname.h                |  2 +-
 lib/tests/test_host_resolve.c |  4 ++--
 lib/tests/test_hostname.c     |  8 ++++----
 5 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/lib/cfg.c b/lib/cfg.c
index d848814..356942a 100644
--- a/lib/cfg.c
+++ b/lib/cfg.c
@@ -343,7 +343,7 @@ cfg_init(GlobalConfig *cfg)
   stats_reinit(&cfg->stats_options);
 
   dns_caching_update_options(&cfg->dns_cache_options);
-  hostname_reinit(cfg->custom_domain);
+  hostname_reinit(cfg->custom_domain, cfg->host_resolve_options.use_fqdn);
   host_resolve_options_init_globals(&cfg->host_resolve_options);
   log_template_options_init(&cfg->template_options, cfg);
   if (!cfg_init_modules(cfg))
diff --git a/lib/hostname.c b/lib/hostname.c
index 075cf5f..38967b2 100644
--- a/lib/hostname.c
+++ b/lib/hostname.c
@@ -40,7 +40,7 @@ static gboolean local_domain_overridden;
 static gchar *
 get_local_hostname_from_system(void)
 {
-  gchar hostname[256];
+  gchar hostname[256] = {0};
 
   gethostname(hostname, sizeof(hostname) - 1);
   hostname[sizeof(hostname) - 1] = '\0';
@@ -124,12 +124,12 @@ convert_hostname_to_short_hostname(gchar *hostname, gsize hostname_len)
 }
 
 static void
-detect_local_fqdn_hostname(void)
+detect_local_fqdn_hostname(gboolean use_fqdn)
 {
   gchar *hostname;
 
   hostname = get_local_hostname_from_system();
-  if (!is_hostname_fqdn(hostname))
+  if (use_fqdn && !is_hostname_fqdn(hostname))
     {
       /* not fully qualified, resolve it using DNS or /etc/hosts */
       g_free(hostname);
@@ -146,6 +146,11 @@ detect_local_fqdn_hostname(void)
             }
         }
     }
+  else if (!hostname[0])
+    {
+      msg_error("Could not resolve local hostname from gethostname(), assuming localhost");
+      hostname = g_strdup("localhost");
+    }
 
   g_strlcpy(local_hostname_fqdn, hostname, sizeof(local_hostname_fqdn));
   g_free(hostname);
@@ -184,9 +189,9 @@ set_domain_override(const gchar *domain_override)
 }
 
 void
-hostname_reinit(const gchar *domain_override)
+hostname_reinit(const gchar *domain_override, gboolean use_fqdn)
 {
-  detect_local_fqdn_hostname();
+  detect_local_fqdn_hostname(use_fqdn);
   detect_local_domain();
   detect_local_short_hostname();
   set_domain_override(domain_override);
@@ -195,7 +200,7 @@ hostname_reinit(const gchar *domain_override)
 void
 hostname_global_init(void)
 {
-  hostname_reinit(NULL);
+  hostname_reinit(NULL, FALSE);
 }
 
 void
diff --git a/lib/hostname.h b/lib/hostname.h
index c81189f..06c5dc2 100644
--- a/lib/hostname.h
+++ b/lib/hostname.h
@@ -32,7 +32,7 @@ gchar *convert_hostname_to_short_hostname(gchar *hostname, gsize hostname_len);
 const gchar *get_local_hostname_fqdn(void);
 const gchar *get_local_hostname_short(void);
 
-void hostname_reinit(const gchar *custom_domain);
+void hostname_reinit(const gchar *custom_domain, gboolean use_fqdn);
 void hostname_global_init(void);
 void hostname_global_deinit(void);
 
diff --git a/lib/tests/test_host_resolve.c b/lib/tests/test_host_resolve.c
index d6489ae..b41d302 100644
--- a/lib/tests/test_host_resolve.c
+++ b/lib/tests/test_host_resolve.c
@@ -229,7 +229,7 @@ Test(resolve_hostname, test_short_hostname_is_converted_to_fqdn_if_use_fqdn_is_s
   /* force the use of custom domain to make asserts easier. the
    * non-custom-domain case is tested by test-hostname.c */
 
-  hostname_reinit("bardomain");
+  hostname_reinit("bardomain", TRUE);
   assert_hostname_to_hostname("foo", "foo.bardomain");
 }
 
@@ -255,7 +255,7 @@ setup(void)
   configuration = cfg_new_snippet();
   host_resolve_options_defaults(&host_resolve_options);
   host_resolve_options_init(&host_resolve_options, &configuration->host_resolve_options);
-  hostname_reinit(NULL);
+  hostname_reinit(NULL, TRUE);
 }
 
 static void
diff --git a/lib/tests/test_hostname.c b/lib/tests/test_hostname.c
index 5ccef21..0d4cecd 100644
--- a/lib/tests/test_hostname.c
+++ b/lib/tests/test_hostname.c
@@ -117,7 +117,7 @@ ParameterizedTest(HostNameList *host_name_list, test_hostname, test_hostname_fqd
   gchar buf[256];
 
   wrap_gethostname();
-  hostname_reinit(host_name_list->domain_override);
+  hostname_reinit(host_name_list->domain_override, TRUE);
 
   g_strlcpy(buf, host_name_list->host_name, sizeof(buf));
   convert_hostname_to_fqdn(buf, sizeof(buf));
@@ -145,7 +145,7 @@ ParameterizedTest(HostNameList *host_name_list, test_hostname, test_hostname_sho
   gchar buf[256];
 
   wrap_gethostname();
-  hostname_reinit(host_name_list->domain_override);
+  hostname_reinit(host_name_list->domain_override, TRUE);
 
   g_strlcpy(buf, host_name_list->host_name, sizeof(buf));
   convert_hostname_to_short_hostname(buf, sizeof(buf));
@@ -169,7 +169,7 @@ ParameterizedTest(HostNameList *host_name_list, test_hostname, test_hostname_fqd
   const gchar *host;
 
   wrap_gethostname();
-  hostname_reinit(host_name_list->domain_override);
+  hostname_reinit(host_name_list->domain_override, TRUE);
 
   host = get_local_hostname_fqdn();
   cr_assert_str_eq(host, host_name_list->expected, "hostname values mismatch");
@@ -192,7 +192,7 @@ ParameterizedTest(HostNameList *host_name_list, test_hostname, test_hostname_sho
   const gchar *host;
 
   wrap_gethostname();
-  hostname_reinit(host_name_list->domain_override);
+  hostname_reinit(host_name_list->domain_override, TRUE);
 
   host = get_local_hostname_short();
   cr_assert_str_eq(host, host_name_list->expected, "hostname values mismatch");
-- 
2.39.0

